package fire;

import java.awt.event.KeyEvent;

import jgame.JGPoint;
import jgame.platform.JGEngine;
import util.OOMLoad;
import view.ViewFollowPlayer;
import view.ViewLoadingScreen;
import view.ViewStart;
import view.ViewState;
import fire.chacracter.Enemy;
import fire.chacracter.Player;
import fire.combat.CombatMap;
import fire.room.FactoryMap;
import fire.room.Map;

public class Main extends JGEngine {
	private static final long serialVersionUID = 8036832741720768202L;
	private static Map map;
	private static Main main;
	private static ViewState view;
	private static ViewState loadingView, followPlayerView, startGameView;
	public static final int VIEW_LOADING = 0, VIEW_FOLLOW_PLAYER = 1,
			VIEW_START_GAME = 2;

	// private Hashtable<String, Map> mapList;

	public Main(JGPoint size) {
		initEngine(size.x, size.y);
		loadingView = new ViewLoadingScreen(this);
		followPlayerView = new ViewFollowPlayer(this);
		startGameView = new ViewStart(this);
		view = startGameView;
		OOMLoad.loadClassList();
	}

	public Main() {
		initEngineApplet();
	}

	public static void main(String[] args) {
		main = new Main(new JGPoint(0, 0));
	}

	@Override
	public void initCanvas() {
		// we set the background colour to same colour as the splash background
		setCanvasSettings(65, 48, 16, 16, null, null, null);
	}

	@Override
	public void initGame() {

		this.getSize();
		setFrameRate(35, 2);
		defineMedia("setting/init.tbl");
		FactoryMap.loadMapKist();
		// tao list pet
		
		map = FactoryMap.mapList.get("startgame");
		map.createMap(main);
		// setPFWrap(true, true, 0, 0);
		// player = new Player("player", 200, 200, "player_d", 3);
	}

	public void doFrame() {
		view.viewUpdate();
		// Move all objects.
		moveObjects(null, 0);
	}

	public static void changeMap(Map newMap) {
		map.suspend();
		map = newMap;
		map.createMap(main);
	}

	public void paintFrame() {
		if (getKey(KeyEvent.VK_Q)) {
			setBGImage(0, "clone", true, true);
		}
		if (getKey(KeyEvent.VK_E)) {
			changeMap(new Map("setting/test2.tbl", "setting/test2.oom", 200,
					200, 101, 101));
		}

		if (getKey(KeyEvent.VK_R)) {
			changeMap(new Map("setting/test2.tbl", "setting/test2.oom", 200,
					200, 102, 101));
		}
	}

	public Map getMap() {
		return map;
	}

	public Player getPlayer() {
		return map.getPlayer();
	}

	public static void setView(int viewCode) {
		switch (viewCode) {
		case VIEW_LOADING:
			view = loadingView;
			break;
		case VIEW_FOLLOW_PLAYER:
			view = followPlayerView;
			break;
		case VIEW_START_GAME:
			view = startGameView;
			break;
		default:
			break;
		}
	}

	public static void combat(Enemy enemy) {
		map.suspend();
		enemy.setResumeMode(false);
		enemy.suspend();
		CombatMap.create(main, enemy, map);
	}

}
